package com.zcf.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zcf.common.json.Body;
import com.zcf.mapper.BannerMapper;
import com.zcf.pojo.Banner;
import com.zcf.pojo.Dining;
import com.zcf.service.BannerService;
import com.zcf.utils.Hutool;
import com.zcf.utils.UploadImgUtils;

@Controller
@RequestMapping("/banner")
@CrossOrigin
@ResponseBody
public class BannerCotroller {
	@Autowired
	BannerService bannerService;
	@Autowired
	BannerMapper bannerMapper;

	/**
	 * 发布轮播图
	 * 
	 * @param banner
	 * @param file
	 * @return
	 */
	@PostMapping("insertBanner")
	public Body insertBanner(Banner banner, MultipartFile[] file) {
		return bannerService.insertBanner(banner, file);
	}

	/**
	 * 修改轮播图
	 */
	@PostMapping("updateBanner")
	public Body updateBanner(Banner banner, String file) {
		Banner banner2 = new Banner();
		banner2.setId(banner.getId());
		banner2 = banner2.selectById();
		if (banner2 == null) {
			return Body.newInstance(-1, "轮播图不存在");
		}
		if (banner.getIntro() != null) {
			banner2.setIntro(banner.getIntro());
		}
		banner2.setDinId(banner.getDinId());
		banner2.setTime(Hutool.parseDateToString());
		try {
//			String files = UploadImgUtils.uploadFiles(file);
			if (file != null && file != "") {
				banner2.setPhth(file);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Boolean flag = banner2.updateById();
		if (flag) {
			return Body.newInstance(1, "修改成功");
		}
		return Body.newInstance(-1, "修改失败");
	}

	/**
	 * 删除轮播图
	 */
	@PostMapping("deledteBanner")
	public Body deledteBanner(String id) {
		EntityWrapper<Banner> ew = new EntityWrapper<>();
		ew.eq("id", id);
		Banner banner = new Banner();
		Boolean falg = banner.delete(ew);
		if (falg) {
			return Body.newInstance(1, "删除成功");
		}
		return Body.newInstance(-1, "删除失败");
	}

	/**
	 * 查询轮播图
	 */
	@PostMapping("queryBanner")
	public Body queryBanner() {
		List<Banner> lists = bannerMapper.queryBanner();
		List<Banner> list = new ArrayList<Banner>();
		for (Banner banner : lists) {
			if (banner.getDinId() != null && !banner.getDinId().equals("")) {
				Dining dining = new Dining();
				dining.setId(banner.getDinId());
				dining = dining.selectById();
				if (dining != null) {
					banner.setDinName(dining.getDiningName());
				}
			}
			if (banner.getType() == 1) {
				banner.setTypes("使用");
			} else {
				banner.setTypes("停用");
			}
			if (!list.contains(banner)) {
				list.add(banner);
			}
		}
		return Body.newInstance(list);
	}

	/**
	 * 停用
	 */
	@PostMapping("updatype")
	public Body updatype(String id) {
		Banner banner = new Banner();
		banner.setId(id);
		banner.setType(0);
		Boolean flag = banner.updateById();
		if (flag) {
			return Body.BODY_200;
		}
		return Body.BODY_500;
	}

	/**
	 * 启用
	 */
	@PostMapping("updateType")
	public Body updateType(String id) {
		Banner banner = new Banner();
		banner.setId(id);
		banner.setType(1);
		Boolean flag = banner.updateById();
		if (flag) {
			return Body.BODY_200;
		}
		return Body.BODY_500;
	}
}
